﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:BARCODELOG
    /// </summary>
    public partial class BARCODELOG
    {
        public BARCODELOG()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from BARCODELOG");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.BigInt)
            };
            parameters[0].Value = F_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.BARCODELOG model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into BARCODELOG(");
            strSql.Append("BARCODEVALUE,STATE,F_UNITID,PTYPEID,LAYER,ABOUTBILL,ABOUTBILLID,ABOUTBILLTYPE,F_DATE,USERID,Instruction)");
            strSql.Append(" values (");
            strSql.Append("@BARCODEVALUE,@STATE,@F_UNITID,@PTYPEID,@LAYER,@ABOUTBILL,@ABOUTBILLID,@ABOUTBILLTYPE,@F_DATE,@USERID,@Instruction)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@BARCODEVALUE", SqlDbType.VarChar, 250),
                    new SqlParameter("@STATE", SqlDbType.Int, 4),
                    new SqlParameter("@F_UNITID", SqlDbType.Int, 4),
                    new SqlParameter("@PTYPEID", SqlDbType.VarChar, 50),
                    new SqlParameter("@LAYER", SqlDbType.Int, 4),
                    new SqlParameter("@ABOUTBILL", SqlDbType.NVarChar, 50),
                    new SqlParameter("@ABOUTBILLID", SqlDbType.Int, 4),
                    new SqlParameter("@ABOUTBILLTYPE", SqlDbType.Int, 4),
                    new SqlParameter("@F_DATE", SqlDbType.DateTime),
                    new SqlParameter("@USERID", SqlDbType.Int, 4),
                    new SqlParameter("@Instruction", SqlDbType.VarChar, 200) };
            parameters[0].Value = model.BARCODEVALUE;
            parameters[1].Value = model.STATE;
            parameters[2].Value = model.F_UNITID;
            parameters[3].Value = model.PTYPEID;
            parameters[4].Value = model.LAYER;
            parameters[5].Value = model.ABOUTBILL;
            parameters[6].Value = model.ABOUTBILLID;
            parameters[7].Value = model.ABOUTBILLTYPE;
            parameters[8].Value = model.F_DATE;
            parameters[9].Value = model.USERID;
            parameters[10].Value = model.Instruction;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.BARCODELOG model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update BARCODELOG set ");
            strSql.Append("BARCODEVALUE=@BARCODEVALUE,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("F_UNITID=@F_UNITID,");
            strSql.Append("PTYPEID=@PTYPEID,");
            strSql.Append("LAYER=@LAYER,");
            strSql.Append("ABOUTBILL=@ABOUTBILL,");
            strSql.Append("ABOUTBILLID=@ABOUTBILLID,");
            strSql.Append("ABOUTBILLTYPE=@ABOUTBILLTYPE,");
            strSql.Append("F_DATE=@F_DATE,");
            strSql.Append("USERID=@USERID,");
            strSql.Append("Instruction=@Instruction");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@BARCODEVALUE", SqlDbType.VarChar, 250),
                    new SqlParameter("@STATE", SqlDbType.Int, 4),
                    new SqlParameter("@F_UNITID", SqlDbType.Int, 4),
                    new SqlParameter("@PTYPEID", SqlDbType.VarChar, 50),
                    new SqlParameter("@LAYER", SqlDbType.Int, 4),
                    new SqlParameter("@ABOUTBILL", SqlDbType.NVarChar, 50),
                    new SqlParameter("@ABOUTBILLID", SqlDbType.Int, 4),
                    new SqlParameter("@ABOUTBILLTYPE", SqlDbType.Int, 4),
                    new SqlParameter("@F_DATE", SqlDbType.DateTime),
                    new SqlParameter("@USERID", SqlDbType.Int, 4),
                    new SqlParameter("@Instruction", SqlDbType.VarChar, 200),
                    new SqlParameter("@F_ID", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.BARCODEVALUE;
            parameters[1].Value = model.STATE;
            parameters[2].Value = model.F_UNITID;
            parameters[3].Value = model.PTYPEID;
            parameters[4].Value = model.LAYER;
            parameters[5].Value = model.ABOUTBILL;
            parameters[6].Value = model.ABOUTBILLID;
            parameters[7].Value = model.ABOUTBILLTYPE;
            parameters[8].Value = model.F_DATE;
            parameters[9].Value = model.USERID;
            parameters[10].Value = model.Instruction;
            parameters[11].Value = model.F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from BARCODELOG ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.BigInt)
            };
            parameters[0].Value = F_ID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_IDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from BARCODELOG ");
            strSql.Append(" where F_ID in (" + F_IDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.BARCODELOG GetModel(long F_ID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ID,BARCODEVALUE,STATE,F_UNITID,PTYPEID,LAYER,ABOUTBILL,ABOUTBILLID,ABOUTBILLTYPE,F_DATE,USERID,Instruction from BARCODELOG ");
            strSql.Append(" where F_ID=@F_ID");
            var parameters = new SqlParameter[] { new SqlParameter("@F_ID", SqlDbType.BigInt)
            };
            parameters[0].Value = F_ID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.BARCODELOG DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.BARCODELOG();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != string.Empty)
                {
                    model.F_ID = long.Parse(row["F_ID"].ToString());
                }
                if (row["BARCODEVALUE"] != null)
                {
                    model.BARCODEVALUE = row["BARCODEVALUE"].ToString();
                }
                if (row["STATE"] != null && row["STATE"].ToString() != string.Empty)
                {
                    model.STATE = int.Parse(row["STATE"].ToString());
                }
                if (row["F_UNITID"] != null && row["F_UNITID"].ToString() != string.Empty)
                {
                    model.F_UNITID = int.Parse(row["F_UNITID"].ToString());
                }
                if (row["PTYPEID"] != null)
                {
                    model.PTYPEID = row["PTYPEID"].ToString();
                }
                if (row["LAYER"] != null && row["LAYER"].ToString() != string.Empty)
                {
                    model.LAYER = int.Parse(row["LAYER"].ToString());
                }
                if (row["ABOUTBILL"] != null)
                {
                    model.ABOUTBILL = row["ABOUTBILL"].ToString();
                }
                if (row["ABOUTBILLID"] != null && row["ABOUTBILLID"].ToString() != string.Empty)
                {
                    model.ABOUTBILLID = int.Parse(row["ABOUTBILLID"].ToString());
                }
                if (row["ABOUTBILLTYPE"] != null && row["ABOUTBILLTYPE"].ToString() != string.Empty)
                {
                    model.ABOUTBILLTYPE = int.Parse(row["ABOUTBILLTYPE"].ToString());
                }
                if (row["F_DATE"] != null && row["F_DATE"].ToString() != string.Empty)
                {
                    model.F_DATE = DateTime.Parse(row["F_DATE"].ToString());
                }
                if (row["USERID"] != null && row["USERID"].ToString() != string.Empty)
                {
                    model.USERID = int.Parse(row["USERID"].ToString());
                }
                if (row["Instruction"] != null)
                {
                    model.Instruction = row["Instruction"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select F_ID,BARCODEVALUE,STATE,F_UNITID,PTYPEID,LAYER,ABOUTBILL,ABOUTBILLID,ABOUTBILLTYPE,F_DATE,USERID,Instruction ");
            strSql.Append(" FROM BARCODELOG ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_ID,BARCODEVALUE,STATE,F_UNITID,PTYPEID,LAYER,ABOUTBILL,ABOUTBILLID,ABOUTBILLTYPE,F_DATE,USERID,Instruction ");
            strSql.Append(" FROM BARCODELOG ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM BARCODELOG ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.F_ID desc");
            }
            strSql.Append(")AS Row, T.*  from BARCODELOG T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

